<?php
/**
 * 留言管理接口
 * @since 2013-03-15
 * @author huanran.zhao
 */
class MessageApp extends ApiCommonApp{
	
	/**
	 * 添加留言
	 * url: message.add
	 * @param string $content 留言内容
	 * @return json
	 */
	public function addAct(){
		//留言类型:问题反馈
		$data['item'] = 'feedback';
		$data['content'] = My::arr2var($_REQUEST,'content');
		$ok = M('message')->addMessage($data);
		if($ok)
			$this->formatReturn(200);
		else
			$this->formatReturn(302);
	}

	/**
	 * 发送邮件
	 * url: message.sendmail
	 * @param string $receiver 收件人(多个用逗号分隔)
	 * @param string $title 主题
	 * @param string $content 内容
	 * @param string $attachments 附件(多个用逗号的图片id)
	 * @return json
	 */
	public function sendmailAct(){
		error_reporting(0);
		set_time_limit (0);
		//print_r(My::user());exit;
		//主题
		$data['title'] = My::arr2var($_REQUEST,'title');
		//内容
		$data['content'] = My::arr2var($_REQUEST,'content');
		//类型
		$data['item'] = 'email';
		//发件人邮箱
		$data['email'] = My::user('email');
		//收件人邮箱
		$data['params']['receiver'] = My::arr2var($_REQUEST,'receiver');
		//生成附件,打包
		if(My::arr2var($_REQUEST,'attachments')!=''){
			$ids = explode(',',My::arr2var($_REQUEST,'attachments'));
			if(is_array($ids))
			foreach($ids as $value){
				if(My::is_id($value)){
					$link = DB('photo')->where("`id`={$value}")->getField('link');
					if(file_exists('../'.$link)){
						$temp = $_SERVER['DOCUMENT_ROOT'].My::thums($link,1024,768);
						if(file_exists($temp)) $filelist[] = $temp;
						//添加文件到压缩zip包
						//$v_list = $archive->add('../'.$link,PCLZIP_OPT_REMOVE_PATH, '../share/images/choice/');//'../share/images/choice/'
					}
				}
			}
			if(is_array($filelist)) $filestr=implode(',', $filelist);
			//压缩类
			$attachments='';
			if(!empty($filestr)){
				$attachments = DATA_DIR."APP_ZIP/email/".uniqid().".zip";//附件名称,唯一id
				$archive = new PclZip($attachments);
				$v_list = $archive->create($filestr,PCLZIP_OPT_REMOVE_PATH,DATA_DIR.'thumbnail/share/images/choice/');//,PCLZIP_OPT_ADD_PATH, 'install'
				if($v_list==0) die("Error : ".$archive->errorInfo(true));
				//附件
				$data['params']['attachments'] = $attachments;
			}
		}
		//序列化参数数组
		$data['params'] = My::encode(serialize($data['params']));
		if(empty($_SESSION['email_attr']['host']) OR empty($_SESSION['email_attr']['port'])){
			$this->formatReturn(351);
		}

		$emailParam['username'] = My::user('email');//发件人的邮箱用户名
		//$emailParam['password'] = My::decode(My::user('email_password'));//发件人的邮箱用户密码
		$emailParam['host'] = $_SESSION['email_attr']['host'];//指定的SMTP服务器地址
		$emailParam['receiver'] = (My::arr2var($_REQUEST,'receiver')!='')?explode(';',My::arr2var($_REQUEST,'receiver')):'';//收件人地址(多个用逗号分隔)
		$emailParam['title'] = My::arr2var($_REQUEST,'title');//邮箱标题
		$emailParam['content'] = My::arr2var($_REQUEST,'content');//邮箱内容
		$emailParam['attachments'] = $attachments;//附件地址
		$sendok = Email::send($emailParam);
		if($sendok==1){
			//添加到数据库
			$ok = M('message')->addMessage($data);
			//return
			$this->formatReturn(200);
		}else{
			if(file_exists($attachments)) unlink($attachments);
			echo $sendok;
			//$this->formatReturn(350);//操作失败
		}
	   
	}

	

}